let app = getApp()

import Notify from '@vant/weapp/notify/notify';

import {
  userProfile
} from "../../api/user/user.js"

import {
  callScanUrl
} from "../../api/common/scan.js"

import {
  getUrlParamsObj
} from "../../utils/util.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    DOMAIN_NAME: app.IMG_NAME,
    userDetail: {},
    // tabBar param
    tabBarParam: {
      selected: 2,
      color: "#515151",
      selectedColor: "#217CFF",
      backgroundColor: "#ffffff",
    },
    // tabBar menu
    tabBarList: [{
        "pagePath": "pages/index/newIndex/newIndex",
        "iconPath": "/images/tabbar/home.png",
        "selectedIconPath": "/images/tabbar/home-select.png",
        "text": "首页"
      },
      {
        "pagePath": "pages/meeting/meetingReservation/meetingReservation",
        "iconPath": "/images/tabbar/openDoor.png",
        "selectedIconPath": "/images/tabbar/openDoor.png",
        "text": "预约",
        "bulge": true,
      },
      {
        "pagePath": "pages/my/my",
        "iconPath": "/images/tabbar/my.png",
        "selectedIconPath": "/images/tabbar/my-select.png",
        "text": "我的"
      },
    ],
    customerPages: [{
        name: "预约记录",
        url: "/pages/meeting/reservationRecord/reservationRecord",
      },
      {
        name: "我的活动",
        url: "/pages/myActivities/myActivities",
      },
      {
        name: "我的入驻",
        url: "/pages/my/serviceApply/serviceApply"
      },
      {
        name: "我的建议",
        url: "/pages/complaint/complaint",
      },
      // {
      //     name: "账号设置",
      //     url: "/pages/my/accountSetting/accountSetting",
      // },
      {
        name: "我的服务",
        url: "/pages/enterpriseServices/myServiceList/myServiceList"
      },
      // {
      //     name: "我的报修",
      //     url: "/pages/parkRepair/parkRepair",
      // }
    ],
    qrcodeParam: null,
  },

  // 切换tabbar
  switchTab(e) {
    const data = e.currentTarget.dataset
    const index = e.currentTarget.dataset.index
    console.log('switchTab', data);
    const url = data.path
    if(index == 1){
      wx.navigateTo({
        url: '/' + url
      });
    }else{
      wx.switchTab({
        url: '/' + url
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  onShow() {
    let _this = this
    let userId = wx.getStorageSync('userId');
    if (userId) {
      userProfile(userId).then(res => {
        console.log('userProfile', res);
        _this.setData({
          userDetail: res.data
        })
      })
    } else {
      app.getlogin()
    }
  },

  navigateTo(e) {
    if (wx.getStorageSync('userId')) {
      let dataset = e.currentTarget.dataset;
      wx.navigateTo({
        url: dataset.url + "?name=" + dataset.name,
      })
    } else {
      app.getlogin()
    }
  },

  navpersonalData() {
    if (wx.getStorageSync('userId')) {
      wx.navigateTo({
        url: '/pages/my/personalData/personalData',
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  // 扫一扫
  jumpScan() {
    let _this = this;
    wx.scanCode({
      scanType: 'qrCode',
      success(res) {
        console.log('success', res)
        let url = res.result;
        // 判断二维码是否符合规范
        if (url.indexOf(app.DOMAIN_NAME_PREFIX) != -1) {
          let urlParam = getUrlParamsObj(url);
          // 判断是否有设备参数
          if (!urlParam.noParam) {
            _this.setData({
              qrcodeParam: urlParam
            })
            // 扫码调用地址
            _this.scanCallUrl()
          } else {
            // 危险通知
            _this.showErrMsg('请预约会议!')
          }
        } else {
          // 危险通知
          _this.showErrMsg('二维码不正确!')
        }
      },
      fail(res) {
        console.log('fail', res)
        // 危险通知
        _this.showErrMsg('扫码失败!')
      }
    })
  },

  // 扫码调用地址
  scanCallUrl() {
    let _this = this;
    let qrcodeParam = _this.data.qrcodeParam;
    qrcodeParam.userId = wx.getStorageSync('userId')
    callScanUrl(qrcodeParam).then(res => {
      // 清空扫码数据
      _this.setData({
        qrcodeParam: null
      })
      if (res.code == 0) {
        // 成功通知
        _this.showSuccessMsg(res.msg)
      } else {
        // 危险通知
        _this.showErrMsg(res.msg)
      }
    })
  },

  // 成功通知
  showSuccessMsg(msg) {
    Notify({
      type: 'success',
      message: msg
    });
  },

  // 危险通知
  showErrMsg(msg) {
    Notify({
      type: 'danger',
      message: msg
    });
  },

})